package models

import (
	"github.com/astaxie/beego/orm"
	"time"
)

func (a *AdminAuthRule) TableName() string {
	return "admin_auth_rule"
}

//管理员权限
type AdminAuthRule struct{
	Id	uint
	Type string `orm:"size(20)"description:"menu为菜单,file为权限节点"`
	Pid uint `description:"父ID"`
	Name string `orm:"size(100)"description:"规则名称"`
	SkipUrl string `orm:"size(50)"description:"转地址"`
	Icon string `orm:"size(255)"description:"图标"`
	Remark string `orm:"null;size(255)"description:"备注"`
	CreatedAt time.Time `orm:"auto_now_add;type(datetime)"description:"创建时间"`
	UpdatedAt time.Time `orm:"null;auto_now;type(datetime)"description:"更新时间"`
	Weigh int `orm:"default(0)"description:"权重"`
	Status uint8 `description:"状态1=正常2=禁用"`
}
//菜单
type TreeMenu struct {
	*AdminAuthRule
	Children []* AdminAuthRule
}

//获取菜单数据
func AdminAuthRuleGetMenu(ruleIds ...string)([]*AdminAuthRule ,error){
	var adminAuthRule []*AdminAuthRule
	qs := orm.NewOrm().QueryTable("admin_auth_rule").Filter("type", "menu").Filter("status",1)
	if len(ruleIds) >0{
		qs = qs.Filter("id__in", ruleIds)
	}
	_,err := qs.OrderBy("weigh").All(&adminAuthRule)
	return adminAuthRule,err
}

//获取所有权限节点
func AdminAuthRuleGetRule()([]*AdminAuthRule ,error){
	var adminAuthRule []*AdminAuthRule
	_,err := orm.NewOrm().QueryTable("admin_auth_rule").OrderBy("weigh").All(&adminAuthRule)
	return adminAuthRule,err
}

//获取根节点
func AdminAuthRuleGetRootRule()([]*AdminAuthRule ,error){
	var adminAuthRule []*AdminAuthRule
	_,err := orm.NewOrm().QueryTable("admin_auth_rule").Filter("pid",0).OrderBy("weigh").All(&adminAuthRule)
	return adminAuthRule,err
}

//根据地址获取权限ID
func AdminAuthRuleGetRuleBySkipUrl(skipUrl string)(AdminAuthRule ,error){
	var adminAuthRule AdminAuthRule
	err := orm.NewOrm().QueryTable("admin_auth_rule").Filter("skip_url", skipUrl).One(&adminAuthRule)
	return adminAuthRule,err
}


